package com.bigfreakindeals.Controller;

import com.bigfreakindeals.R.drawable;
import com.bigfreakindeals.Application.ApplicationEx;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class GPSController {
  
   static GPSController instance = null;
   public Double currentLatitude = 0.00;
   public Double currentLongitude = 0.00;
	Context context;
	GPSLocationListener listener;
	      private Handler splashHandler = new Handler() {
	                      @Override
	                      public void handleMessage(Message msg) {
	                             
	                              super.handleMessage(msg);
	                      }
	              };

	
	
	private GPSController(){
	 
   }
   public void setContext(Context context, GPSLocationListener listener){
	   this.context = context;
	   this.listener = listener;
	   
   }
	public static GPSController getInstance(){
		if (instance == null)
			instance = new GPSController();
		return instance;
	}
	
	
	public void findMe(){
		LocationManager locationManager = null;
		LocationListener ll = null;
	try{
		locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
	    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
	 
	    String provider;
	    Criteria criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(provider);

		// Initialize the location fields
		if (location != null) {
			double lat = (double) (location.getLatitude());
			double longi = (double) (location.getLongitude());
			listener.handlerResponse(true, longi, lat);
	    }
		else 
			listener.handlerResponse(false, (double)0,(double)0);
	}
	catch (Exception e) {
		
		listener.handlerResponse(false, (double)0,(double)0);
    	
	}
	}
		
		
	
}
